package io.renren.modules.password.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.password.dto.PasswordInfoDTO;
import io.renren.modules.password.service.PasswordInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @Author huan.xu
 * @Date 2022/11/2 11:07
 * @Description
 */
@RestController
@RequestMapping("/password/info")
@Api(tags = "密码信息控制器")
public class PasswordInfoController {

  @Autowired
  private PasswordInfoService passwordInfoService;

  /**
   * 列表查询
   */
  @GetMapping("page")
  @ApiOperation("分页")
  @ApiImplicitParams({
      @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
      @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
      @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "title", value = "标题", paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "account", value = "帐号", paramType = "query", dataType = "String")
  })
  @RequiresPermissions("password:info:page")
  public Result<PageData<PasswordInfoDTO>> page(
      @ApiIgnore @RequestParam Map<String, Object> params) {
    PageData<PasswordInfoDTO> page = passwordInfoService.page(params);
    return new Result<PageData<PasswordInfoDTO>>().ok(page);
  }

  @GetMapping("{id}")
  @ApiOperation("根据id查询信息")
  @RequiresPermissions("password:info")
  public Result<PasswordInfoDTO> get(@PathVariable("id") Long id) {
    PasswordInfoDTO data = passwordInfoService.getById(id);

    return new Result<PasswordInfoDTO>().ok(data);
  }

  @PostMapping
  @ApiOperation("保存")
  @LogOperation("保存")
  @RequiresPermissions("password:info:save")
  public Result save(@RequestBody PasswordInfoDTO photoLabelDTO) {
    //效验数据
    ValidatorUtils.validateEntity(photoLabelDTO, AddGroup.class);

    Result<String> saveResult = null;
    try {
      saveResult = passwordInfoService.save(photoLabelDTO);
    } catch (Exception e) {
      saveResult = new Result<String>().error(e.getMessage());
    }
    return saveResult;
  }

  @PutMapping
  @ApiOperation("修改")
  @LogOperation("修改")
  @RequiresPermissions("password:info:update")
  public Result update(@RequestBody PasswordInfoDTO dto) {
    //效验数据
    ValidatorUtils.validateEntity(dto, UpdateGroup.class);

    Result<String> saveResult = null;
    try {
      saveResult = passwordInfoService.update(dto);
    } catch (Exception e) {
      saveResult = new Result<String>().error(e.getMessage());
    }
    return saveResult;
  }

  @DeleteMapping
  @ApiOperation("删除")
  @LogOperation("删除")
  @RequiresPermissions("password:info:delete")
  public Result delete(@RequestBody Long[] ids) {
    //效验数据
    AssertUtils.isArrayEmpty(ids, "id");

    passwordInfoService.delete(ids);

    return new Result();
  }
}
